package com.gxa.orderManagement.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.R;
import com.gxa.common.entity.orderManagement.OrderFinder;
import com.gxa.common.entity.orderManagement.SysOrder;
import com.gxa.common.entity.usermange.VipUser;
import com.gxa.orderManagement.feign.UserFeign;
import com.gxa.orderManagement.mapper.OrderManagementMapper;
import com.gxa.orderManagement.service.OrderManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderManagementServiceImpl extends ServiceImpl<OrderManagementMapper, SysOrder> implements OrderManagementService{

    @Autowired
    private OrderManagementMapper orderManagementMapper;

    @Autowired
    private UserFeign userFeign;

    @Override
    public R findOrdersByOrderFinder(OrderFinder orderFinder) {
        R r = new R();

        QueryWrapper<SysOrder> queryWrapper = new QueryWrapper<>();
        Page<SysOrder> page = new Page<>(orderFinder.getCurrentPage(),orderFinder.getPageSize());
        System.out.println("=====" + page.getCurrent() + "======" + page.getSize());
        queryWrapper.eq(orderFinder.getOrderType() != null,"order_type", orderFinder.getOrderType());
        queryWrapper.eq(orderFinder.getOrderId() != null, "order_id", orderFinder.getOrderId());
        if (orderFinder.getConsumerNickname() != null){
            List<Long> userIds = userFeign.findIdsByNickname(orderFinder.getConsumerNickname());
            queryWrapper.in("customer_id", userIds);
        }


        Page<SysOrder> sysOrderPage = orderManagementMapper.selectPage(page, queryWrapper);
        System.out.println(sysOrderPage.getRecords());
        System.out.println(sysOrderPage.getTotal());
        System.out.println(sysOrderPage.getRecords().size());


        r.setCode(200);
        if (sysOrderPage.getRecords().size() <= 0){
            r.setMessage("没有符合条件的查询结果哦！");
        }else {
            r.setMessage("查询成功");
        }
        r.setData(sysOrderPage.getRecords());
        return r;
    }

    public R findOrderPages(OrderFinder orderFinder){
        R r = new R();
        R ordersByOrderFinder = findOrdersByOrderFinder(orderFinder);
        List<SysOrder> sysOrders = (List<SysOrder>) ordersByOrderFinder.getData();

        List<Map<String,Object>> orderManagementDetails = new ArrayList<>();
        for (SysOrder sysOrder:sysOrders) {
            Map<String,Object> orderManagementDetail = new HashMap<>();
            Map<String,Object> orderDetail = new HashMap<>();
            orderDetail.put("orderId",sysOrder.getOrderId());
            orderDetail.put("createTime",sysOrder.getCreateTime());
            orderDetail.put("orderType",sysOrder.getOrderType());
            orderDetail.put("deliveryTime",sysOrder.getDeliveryTime());
            orderDetail.put("shippingMethod",sysOrder.getShippingMethod());
            orderDetail.put("tableware",sysOrder.getTableware());
            orderDetail.put("remarks",sysOrder.getRemarks());
            orderDetail.put("hasInvoice",sysOrder.getHasInvoice());
            orderDetail.put("totalPrice",sysOrder.getTotalPrice());
            orderDetail.put("packingFee",sysOrder.getPackingFee());
            orderDetail.put("deliveryFee",sysOrder.getDeliveryFee());
            orderDetail.put("totalDiscount",sysOrder.getTotalDiscount());
            orderDetail.put("accountsPayable",sysOrder.getAccountsPayable());
            orderDetail.put("paymentMethod",sysOrder.getPaymentMethod());
            orderDetail.put("estimatedRevenue",sysOrder.getEstimatedRevenue());
            orderDetail.put("orderStatus",sysOrder.getOrderStatus());
            orderDetail.put("refundStatus",sysOrder.getRefundStatus());
            orderManagementDetail.put("orderDetail", orderDetail);

//            R allById = userFeign.findAllById(sysOrder.getCustomerId());
//            VipUser customerDetail = (VipUser) allById.getData();
            VipUser allById = userFeign.findAllById(sysOrder.getCustomerId());
            orderManagementDetail.put("customerDetail",allById);
            orderManagementDetails.add(orderManagementDetail);
        }

        r.setCode(200);
        r.setMessage("查询成功");
        r.setData(orderManagementDetails);
        return r;
    }


}
